// pages/player/player.js


let musiclist = Array
let nowPkayingIndex = 0
//获取全局唯一的背景音频管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: "",
    isPlaying: false,
    isLyricShow: false, //当前歌词是否显示
    lyric: "", //歌词初始化
    isSame: false //是否为同一首歌曲
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    nowPkayingIndex = options.index
    musiclist = wx.getStorageSync('musiclist')
    this._loadMuiscDetail(
      options.musicId
    )
  },
  _loadMuiscDetail(musicId) {
    if (app.getPlayMusicId() == musicId) {
      this.setData({
        isSame: true
      })
    } else {
      this.setData({
        isSame: false
      })
    }

    wx.showLoading({
      title: '歌曲加载中',
    })
    let music = musiclist[nowPkayingIndex]
    console.log(music)
    wx.setNavigationBarTitle({
      title: music.name,
    })
    this.setData({
      picUrl: music.al.picUrl
    })
    wx.cloud.callFunction({
      name: "music",
      data: {
        $url: "musicUrl",
        musicId: musicId
      }
    }).then((res) => {
      let result = JSON.parse(res.result)
      console.log(result.data[0].url)
      if(result.data[0].url == null ){
        wx.showToast({
          title: '暂无权限播放',
        })
        return
      }
      if (!this.data.isSame) {
        backgroundAudioManager.stop()
        backgroundAudioManager.src = result.data[0].url
        backgroundAudioManager.title = music.name
      }
      this.setData({
        isPlaying: true
      })
      wx.hideLoading({
        success: (res) => {},
      })
      app.setPlayMusicId(musicId)
      //加载歌词

      wx.cloud.callFunction({
        name: 'music',
        data: {
          musicId,
          $url: "lyric"
        }
      }).then((res) => {
        console.log(">>>>>>", res)
        let lyric = "暂无歌词"
        const lrc = JSON.parse(res.result).lrc
        if (lrc) {
          lyric = lrc.lyric
        }
        this.setData({
          lyric
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  togglePlaying() {
    if (this.data.isPlaying) {
      backgroundAudioManager.pause()
      this.setData({
        isPlaying: false
      })
    } else {
      backgroundAudioManager.play()
      this.setData({
        isPlaying: true
      })
    }
  },
  onPlay() {
    this.setData({
      isPlaying: true
    })
  },
  onPause() {
    this.setData({
      isPlaying: false
    })
  },
  onPrev() {
    nowPkayingIndex--
    if (nowPkayingIndex < 0) {
      nowPkayingIndex = musiclist.length - 1
    }
    this._loadMuiscDetail(musiclist[nowPkayingIndex].id)
  },
  next() {
    nowPkayingIndex++
    if (nowPkayingIndex === musiclist.length) {
      nowPkayingIndex = 0
    }
    this._loadMuiscDetail(musiclist[nowPkayingIndex].id)

  },
  onChangeLyricShow() {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },
  timeUpdate(event) {
    this.selectComponent(".lyric").update(event.detail.currentTime)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})